package com.module.framework.handler.exception;

import com.module.framework.domain.ResponseResult;
import com.module.framework.enums.AppHttpCodeEnum;
import com.module.framework.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 *
 * @Author JiHuiQin
 * @Date 2022/2/14 22:46
 * @Version 1.0
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 自定义异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(SystemException.class)
	public ResponseResult systemExceptionHandler(SystemException e) {
		log.error("自定义异常", e);
		return ResponseResult.errorResult(e.getCode(), e.getMsg());
	}

	/**
	 * 空指针异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(NullPointerException.class)
	public ResponseResult nullPointerExceptionHandler(NullPointerException e){
		log.error("空指针异常", e);
		return ResponseResult.errorResult(AppHttpCodeEnum.BODY_NOT_MATCH);
	}

	/**
	 * 其他异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public ResponseResult exceptionHandler(Exception e) {
		log.error("其他异常", e);
		return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
	}
}
